package scigest.publish.solr;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import esg.search.publish.api.MetadataRepositoryType;
import esg.search.publish.api.PublishingService;
import esg.search.publish.impl.PublishingServiceMain;

/**
 * This class is mainly derived from esg-search package, but its direct use is 
 * discouraged as we we intend to use ezpub to wrap it.
 * 
 * For ezpub point of view: the following publishing options are available
 * 
 *  <ol>
 *  
 *  <li> To unpublish a single record: <p>
 *  	<code> SolrPublisher id </code>, 
 *  	where "id" is the Solr document id </li>
 *  
 *  <li> To publish or unpublish a remote metadata repository:<p>
 *     <code> SolrPublisher Metadata_Repository_URL OAI|THREDDS|FGDC true|false </code>
 *     where true is publish, false is unpublish
 *    
 *  </ol>
 *  
 *  <p>
 *  
 *  Example - publish a local catalog file <p>
 *  <code>
 *     SolrPublisher file:///Users/fwang2/sample.xml THREDDS true
 *  </code>   
 *     
 *  Example - publish a remote catalog file <p>
 *  <code> 
 *     SolrPublisher http://promus.ccs.ornl.gov/thredds/esgcet/catalog.xml THREDDS true
 *  </code>
 *  
 * @author Feiyi Wang
 *
 */
public class SolrPublisher {
	
    private static final Logger logger = LoggerFactory.getLogger(SolrPublisher.class);
    
	public static void main(String[] args) throws Exception {
		
		// create and configure beans
		ApplicationContext ctx =
		    new ClassPathXmlApplicationContext(new String[] {"scigest.xml"});
		
	    final PublishingService publishingService = (PublishingService) ctx.getBean("publishingService");
	    
	    final SolrPublisher self = new SolrPublisher();
	    self.run(publishingService, args);
	    logger.info("Publishing task is done!");
	}
	    
	    
	/**
	 * Method to execute the web service invocation.
	 * 
	 * @param publishingService
	 * @param args
	 * @throws Exception
	 */
    protected void run(final PublishingService publishingService, final String[] args) throws Exception {
    	
	    if (args.length!=1 && args.length!=3) {
	    	logger.error("Either 1 or 3 arguments are needed. See javadoc for more usage examples");
	    	System.exit(1);
	    }
	    
	    
	    // unpublish single record
	    if (args.length==1) {
	    	final String id = args[0];
	    	publishingService.unpublish(Arrays.asList(new String[] {id}));
	    	
	    // publish/unpublish full repository
	    } else if (args.length==3) {
	    	
		    final String uri = args[0];
		    final MetadataRepositoryType type = MetadataRepositoryType.valueOf(args[1]);
		    
		    //change the Metadata file's URL
		    PublishingServiceMain.METADATA_URL = uri;
		    
		    
		    final boolean publish = Boolean.parseBoolean(args[2]);
		    
		    if (publish) {
		    	publishingService.publish(uri, true, type);
		    } else {
		    	publishingService.unpublish(uri, true, type);
		    }
	    }
		
	}
		
}
